$(function () {

    var ex_datagrid = $("#ex_datagrid");
    var ex_datagrid2 = $("#ex_datagrid2");
    var ex_datagrid3 = $("#ex_datagrid3");
    var ex_dialog = $("#ex_dialog");

    //礼品列表初始化-begin
    $("#ex_datagrid2").datagrid({
        width: 550,
        height: 500,
        url: "/gift/list",
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        columns: [[
            {field: "id", title: "礼品编号", hidden: true},
            {field: "name", title: "礼品名称", width: 50},
            {field: "sn", title: "礼品编码", width: 50},
            {field: "integral", title: "所需积分", width: 50, sortable: true, order: "desc"},
            {field: "number", title: "礼品库存", width: 50}
        ]]
    })

    $("#ex_datagrid3").datagrid({
        fit: true,
        url: "/exchangeGift/list",
        fitColumns: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        columns: [[
            {field: "id", title: "兑换编号", hidden: true},
            {field: "memberinfoCardno", title: "会员卡号", width: 50, align: "center"},
            {field: "memberinfoName", title: "会员姓名", width: 50, align: "center"},
            {field: "giftName", title: "礼品名称", width: 50, align: "center"},
            {field: "number", title: "兑换数量", width: 50, align: "center"},
            {
                field: "giftIntegral", title: "兑换积分", width: 50, align: "center", formatter: function (value) {
                return "<font color='red'>" + value + "</font>"
            }
            },
            {field: "memberinfoAvailablepoint", title: "剩余积分", width: 50, align: "center"},
            {field: "exchangedate", title: "兑换时间", width: 50, align: "center", sortable: true, order: "desc"}
        ]]
    })


    //选择礼品的dialog
    $("#ex_dialog").dialog({
        width: 600,
        height: 500,
        buttons: "#bb",
        draggable: true,
        resizable: true,
        closed: true,
        modal: true,
        onClose: function () {
            $("#ex_form").form("clear");
        }
    })

    /* var cmdObj = {
     fn:function(){
     alert(1);
     },
     name:"dafei"
     }
     cmdObj.fn();
     cmdObj["fn"]();
     cmdObj["name"];*/

    //同一管理响应函数
    var cmdObj = {
        //底部确认按钮
        selected: function () {
            //是否选中row
            var row = ex_datagrid2.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要兑换的礼品");
                return;
            }
            //清空上次操作的数据
            $("#ex_form").form("clear");
            $("#num").textbox("setValue", '');
            //回显选中的行
            $("#ex_form2").form("load", row);
            //关闭窗口
            ex_dialog.dialog("close");

        },
        //选择添加礼品
        addGift: function () {
            //添加前将数据清空
            $("#ex_form").form("clear")
            //设置标题
            ex_dialog.dialog("setTitle", "选择礼品");
            //打开窗口前先清空搜索框
            $("#keyword").textbox("setValue", '');
            //并调用查询方法,显示所有数据
            cmdObj[$("#query").data('cmd')]();
            //打开窗口
            ex_dialog.dialog("open");


        },
        //查询会员信息
        queryByCardno: function () {
            var cardno = $("#number").textbox("getValue")
            //发请求通过会员卡号查询会员信息
            $.get("/exchangeGift/queryByCardno?cardno=" + cardno, function (data) {
                $("#member_form").form("load", data)
                var levelName = data.level.levelName;

                $("#level").textbox("setValue", levelName);

                $("#level").textbox("setValue",levelName);

            })
        },
        //礼品列表:高级查询
        query: function () {
            //获取查询参数
            var kw = $("#keyword").textbox("getValue");
            //表格加载时顺带一起传递查询参数
            ex_datagrid2.datagrid("load", {
                keyword: kw
            });
        },
        //积分兑换按钮
        exchange: function () {
            var totalCount = $("#totalCount").textbox("getValue");
            var cardno = $("#number").val();
            var currentPoint = $("#currentPoint").textbox("getValue");
            var total = $("#total").textbox("getValue");
            var num = $("#num").textbox("getValue");
            var giftId = $("#giftId").val();
            var url = "/exchangeGift/pointsChange";
            if (!totalCount) {
                $.messager.alert("温馨提示", "请先选择要兑换的礼品");
                return;
            }

            /* if (total < num) {
             $.messager.alert("温馨提示", "该礼品库存不足");
             return;
             }

             if (currentPoint < totalCount) {
             $.messager.alert("温馨提示", "积分不足");
             return;
             }*/

            $.get(url, {totalCount: totalCount, cardno: cardno, giftId: giftId}, function (data) {
                if (!data.success) {
                    $.messager.alert('温馨提示', data.errorMsg);
                    //报错清空输入数据
                    $("#num").textbox("setValue", '');
                } else {
                    $.messager.alert('温馨提示', '兑换成功');
                    //清除会员信息前拿到会员卡号
                    var cardno = $("#number").textbox("getValue");
                    //清除数据
                    $("#member_form").form('clear');
                    $("#ex_form2").form('clear');
                    //根据会员卡号重新刷新剩余积分
                    $("#number").textbox("setValue", cardno);
                    cmdObj[$("#query2").data('cmd')]();
                    ex_datagrid3.datagrid("reload");
                }
            })
        },
        //底部按钮:取消
        cancel: function () {
            ex_dialog.dialog("close");
        }
    } //end

    //调用cmdObj
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    })

    //计算兑换消耗积分
    $("#num").textbox({
        onChange: function () {
            var num = parseFloat($("#num").val()
                || 0);
            var integral = parseFloat($("#integral").textbox('getValue')
                || 0);
            var amount = num * integral;
            $("#totalCount").textbox('setValue', amount);
        }
    })

})

